package com.wodan.zhongjinjicang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wodan.zhongjinjicang.common.exception.BusinessException;
import com.wodan.zhongjinjicang.common.utils.JsonUtils;
import com.wodan.zhongjinjicang.common.utils.PasswordDes;
import com.wodan.zhongjinjicang.enums.BusinessErrorEnum;
import com.wodan.zhongjinjicang.enums.YesOrNoEnum;
import com.wodan.zhongjinjicang.frame.AbstractClientApiJsonBusinessMethodService;
import com.wodan.zhongjinjicang.mapper.UserMapper;
import com.wodan.zhongjinjicang.mapper.VerificationMapper;
import com.wodan.zhongjinjicang.model.VerificationBean;
import com.wodan.zhongjinjicang.modeldto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;


/**
 * 用户忘记密码
 */
@Service("forgetPwd")
public class UserForgetPwdService extends AbstractClientApiJsonBusinessMethodService {


    private static final String PHONE = "phone";
    private static final String RAND_PWD = "randPwd";
    private static final String NEW_PWD = "newPwd";
    private static final String USER_ID = "id";


    @Autowired
    private UserMapper userDao;

    @Autowired
    private VerificationMapper verificationDao;


    @Override
    protected JSONObject doExecute(JSONObject data, String sessionId) {
        UserDTO userDTO = JsonUtils.jsonToPojo(data.toJSONString(), UserDTO.class);

        int recordnum = 0;
        // 手机号是否申请过验证码
        VerificationBean v = verificationDao.selectByPhone(userDTO.getPhone());
        if (v != null) {
            Calendar c = Calendar.getInstance();
            //验证码是否过期
            if (v.getExpireTime().after(c.getTime())) {
                //验证码是否正确
                if (v.getValidCode().equals(userDTO.getRandPwd())) {
                    recordnum = editPassword(userDTO);
                    //修改是否成功
                    if (recordnum > 0) {
                        return new JSONObject();
                    }else{
                        throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "修改失败");
                    }
                }else{
                    throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "验证码不正确");
                }
            }else{
                verificationDao.updateCodeStatus(YesOrNoEnum.NO.getCode(),v.getId());
                throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "验证码已经过期");
            }
        }else{
            throw new BusinessException(BusinessErrorEnum.ERROR.getCode(), "手机未申请验证码");
        }

    }


    private int editPassword(UserDTO userDTO) {
        userDTO.setPassword(PasswordDes.makeMD5(userDTO.getNewPwd()));
        return userDao.updatePwdByUserId(userDTO.getId(), userDTO.getPassword());
    }

    @Override
    protected boolean isNeedLogin() {
        return false;
    }

    @Override
    protected String[] getRequiredParams() {
        return new String[]{RAND_PWD,PHONE,NEW_PWD,USER_ID};
    }

}
